package com.pzhu.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.pzhu.entity.*;
import com.pzhu.service.OnlineService;
import com.pzhu.service.UserService;
import com.pzhu.until.JwtTokenUntil;
import com.pzhu.until.TokenClaim;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.time.LocalDate;
import java.util.Date;

@RestController
@RequestMapping("api/user")
@Slf4j
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private OnlineService onlineService;

    /**
     * 用户登录
     * @param session
     * @param user
     * @return
     */
    @PostMapping("/login")
    public Result login(HttpSession session, @RequestBody User user) {
        String code = (String) session.getAttribute("code");
        LambdaQueryWrapper<User> userLambdaQueryWrapper = new LambdaQueryWrapper<>();
        userLambdaQueryWrapper.eq(User::getUserName, user.getUserName());
        User one = userService.getOne(userLambdaQueryWrapper);
        log.info("登录之后的对象是:" + one);
        if (code.equalsIgnoreCase(user.getCode())) {
            if (one != null) {
                if (one.getUserPassword().equals(user.getUserPassword())) {
//                    session.setAttribute("loginUser", one.getUserId());
                    String token = JwtTokenUntil.generate(new TokenClaim("loginUser", one.getUserId()));
                    UpdateWrapper<Online> onlineUpdateWrapper = new UpdateWrapper<>();
                    onlineUpdateWrapper.setSql("logincount = logincount + 1");
                    onlineUpdateWrapper.between("date", LocalDate.now().atStartOfDay(), LocalDate.now().atTime(23, 59, 59));
                    boolean update = onlineService.update(onlineUpdateWrapper);
                    if (!update) {
                        Online online = new Online(new Date(System.currentTimeMillis()), 0, 1);
                        onlineService.save(online);
                    }
                    return new Result(Code.success, "登录成功", token);
                } else {
                    return new Result(Code.error, "密码错误");
                }
            } else {
                return new Result(Code.error, "账号不存在");
            }
        } else {
            return new Result(Code.error, "验证码错误");
        }
    }

    /**
     * 用户注册
     * @param userRegister
     * @return
     */
    @PostMapping("register")
    public Result register(UserRegister userRegister) {
        if (userRegister.getUserPassword().equals(userRegister.getConfirmPassword())) {
            QueryWrapper<User> userRegisterQueryWrapper = new QueryWrapper<>();
            userRegisterQueryWrapper.eq(userRegister != null, "username", userRegister.getUserName());
            User one = userService.getOne(userRegisterQueryWrapper);
            if (one == null) {
                boolean save = userService.save(userRegister);
                return new Result(1, "注册成功", save);
            }
            return new Result(0, "账号已存在");
        }
        return new Result(-1, "两次密码不同");
    }
}
